<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright   Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

?>
<?php
/**
 * @var $this \Magento\Sales\Block\Adminhtml\Order\Create\Items\Grid
 */
?>

<?php $_items = $this->getItems() ?>
<?php if (empty($_items)): ?>
    <div class="grid" id="order-items_grid">
        <table cellspacing="0" class="data-table order-tables">
            <thead>
                <tr class="headings">
                    <th class="col-product"><span><?php echo __('Product') ?></span></th>
                    <th class="col-price"><span><?php echo __('Price') ?></span></th>
                    <th class="col-qty"><span><?php echo __('Qty') ?></span></th>
                    <th class="col-subtotal"><span><?php echo __('Subtotal') ?></span></th>
                    <th class="col-discount"><span><?php echo __('Discount') ?></span></th>
                    <th class="col-row-total"><span><?php echo __('Row Subtotal') ?></span></th>
                    <th class="col-action"><span><?php echo __('Action') ?></span></th>
                </tr>
            </thead>
            <tbody>
                <tr class="even">
                    <td class="empty-text" colspan="100"><?php echo __('No ordered items') ?></td>
                </tr>
            </tbody>
        </table>
    </div>
<?php else: ?>

<div class="grid" id="order-items_grid">
    <div class="hor-scroll">
    <?php if(count($_items)>10): ?>
        <div class="actions update">
            <?php echo $this->getButtonHtml(__('Update Items and Qty\'s'),'order.itemsUpdate()'); ?>
        </div>
    <?php endif; ?>
        <table cellspacing="0" class="data-table order-tables">
            <thead>
                <tr class="headings">
                    <th class="col-product" colspan="2"><span><?php echo __('Product') ?></span></th>
                    <th class="col-price"><span><?php echo __('Price') ?></span></th>
                    <th class="col-qty"><span><?php echo __('Qty') ?></span></th>
                    <th class="col-subtotal"><span><?php echo __('Subtotal') ?></span></th>
                    <th class="col-discount"><span><?php echo __('Discount') ?></span></th>
                    <th class="col-row-total"><span><?php echo __('Row Subtotal') ?></span></th>
                    <th class="col-action"><span><?php echo __('Action') ?></span></th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <td class="col-total" colspan="2"><?php echo __('Total %1 product(s)', count($_items)) ?></td>
                    <td colspan="2" class="col-subtotal"><?php echo __('Subtotal:') ?></td>
                    <td class="col-price"><strong><?php echo $this->formatPrice($this->getSubtotal()) ?></strong></td>
                    <td class="col-price"><strong><?php echo $this->formatPrice($this->getDiscountAmount()) ?></strong></td>
                    <td class="col-price"><strong>
                    <?php
                        echo $this->formatPrice($this->getSubtotal() + $this->getDiscountAmount());
                    ?></strong></td>
                    <td colspan="2">&nbsp;</td>
                </tr>
            </tfoot>
                <?php $i=0 ?>
                <?php foreach ($_items as $_item):$i++ ?>
                <tbody class="<?php echo ($i%2)?'even':'odd' ?>">
                    <tr>
                        <td class="col-product">
                            <span id="order_item_<?php echo $_item->getId() ?>_title"><?php echo $this->escapeHtml($_item->getName()) ?></span>
                            <div>
                                <strong><?php echo __('SKU') ?>:</strong>
                                <?php echo implode('<br />', $this->helper('Magento\Catalog\Helper\Data')->splitSku($this->escapeHtml($_item->getSku()))); ?>
                            </div>
                            <?php if($_item->getMessage(false)): ?>
                                <?php foreach ($_item->getMessage(false) as $message): ?>
                                <div class="<?php if($_item->getHasError()): ?>error<?php else: ?>notice<?php endif; ?>">
                                    <?php echo $this->escapeHtml($message); ?>
                                </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </td>
                        <td class="col-configure">
                            <?php echo $this->getConfigureButtonHtml($_item) ?>
                        </td>
                        <td class="col-price">

                            <?php echo $this->getItemUnitPriceHtml($_item); ?>

                            <?php $_isCustomPrice = $this->usedCustomPriceForItem($_item) ?>
                            <?php if($_tier = $this->getTierHtml($_item)): ?>
                            <div id="item_tier_block_<?php echo $_item->getId() ?>"<?php if ($_isCustomPrice): ?> style="display:none"<?php endif; ?>>
                                <a href="#" onclick="$('item_tier_<?php echo $_item->getId() ?>').toggle();return false;"><?php echo __('Tier Pricing') ?></a>
                                <div style="display:none" id="item_tier_<?php echo $_item->getId() ?>"><?php echo $_tier ?></div>
                            </div>
                            <?php endif; ?>
                            <?php if ($this->canApplyCustomPrice($_item)): ?>
                            <div class="nobr">
                            <input type="checkbox" id="item_use_custom_price_<?php echo $_item->getId() ?>"<?php if ($_isCustomPrice): ?> checked="checked"<?php endif; ?> onclick="order.toggleCustomPrice(this, 'item_custom_price_<?php echo $_item->getId() ?>', 'item_tier_block_<?php echo $_item->getId() ?>');"/>
                            <label class="normal" for="item_use_custom_price_<?php echo $_item->getId() ?>"><?php echo __('Custom Price') ?>*</label>
                            </div>
                            <?php endif; ?>
                            <input id="item_custom_price_<?php echo $_item->getId() ?>" name="item[<?php echo $_item->getId() ?>][custom_price]" value="<?php echo sprintf("%.2f", $this->getOriginalEditablePrice($_item))?>"<?php if (!$_isCustomPrice): ?> style="display:none" disabled="disabled"<?php endif; ?> class="input-text item-price"/>
                        </td>
                        <td class="col-qty"><input name="item[<?php echo $_item->getId() ?>][qty]" class="input-text item-qty" value="<?php echo $_item->getQty()*1 ?>" maxlength="12" /></td>
                        <td class="col-subtotal col-price">
                            <?php echo $this->getItemRowTotalHtml($_item); ?>
                        </td>
                        <td class="col-discount col-price">
                            <?php echo $this->formatPrice(-$_item->getDiscountAmount()) ?><br />
                            <input id="item_use_discount_<?php echo $_item->getId() ?>" name="item[<?php echo $_item->getId() ?>][use_discount]"<?php if (!$_item->getNoDiscount()): ?>checked="checked"<?php endif; ?> value="1" type="checkbox" />
                            <label for="item_use_discount_<?php echo $_item->getId() ?>" class="normal"><?php echo __('Apply') ?></label>
                        </td>
                        <td class="col-price col-row-subtotal">
                            <?php echo $this->getItemRowTotalWithDiscountHtml($_item); ?>
                        </td>
                        <td class="col-actions last">
                            <select name="item[<?php echo $_item->getId() ?>][action]" style="width:100px;">
                                <option value=""></option>
                                <option value="remove"><?php echo __('Remove') ?></option>
                                <?php if($this->getCustomerId() && $this->getMoveToCustomerStorage()): ?>
                                    <option value="cart"><?php echo __('Move to Shopping Cart') ?></option>
                                    <?php if ($this->isMoveToWishlistAllowed($_item)): ?>
                                        <?php $wishlists = $this->getCustomerWishlists();?>
                                        <?php if (count($wishlists) <= 1):?>
                                            <option value="wishlist"><?php echo __('Move to Wishlist') ?></option>
                                        <?php else: ?>
                                            <optgroup label="<?php echo __('Move to Wishlist') ?>">
                                                <?php foreach ($wishlists as $wishlist):?>
                                                    <option value="wishlist_<?php echo $wishlist->getId();?>"><?php echo $this->escapeHtml($wishlist->getName());?></option>
                                                <?php endforeach;?>
                                            </optgroup>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                <?php endif; ?>
                            </select>
                        </td>
                    </tr>
                    <?php echo $this->getItemExtraInfo($_item)->toHtml(); ?>
                </tbody>
                <?php endforeach; ?>
        </table>
        <p><small><?php echo $this->getInclExclTaxMessage(); ?></small></p>
        <p><?php echo $this->getButtonHtml(__('Update Items and Qty\'s'),'order.itemsUpdate()'); ?></p>
    </div>
    <div class="order-coupons" id="order-coupons"><?php echo $this->getChildHtml();?></div>
    <script type="text/javascript">
require([
    'Magento_Sales/order/create/form'
], function(){
    order.itemsOnchangeBind()
});
</script>
</div>

<?php if ($this->isGiftMessagesAvailable()) : ?>
<script type="text/javascript">
require([
    "prototype",
    "Magento_Sales/order/giftoptions_tooltip"
], function(){

//<![CDATA[
    /**
     * Retrieve gift options tooltip content
     */
    function getGiftOptionsTooltipContent(itemId) {
        var contentLines = [];
        var headerLine = null;
        var contentLine = null;

        $$('#gift_options_data_' + itemId + ' .gift-options-tooltip-content').each(function (element) {
            if (element.down(0)) {
                headerLine = element.down(0).innerHTML;
                contentLine = element.down(0).next().innerHTML;
                if (contentLine.length > 30) {
                    contentLine = contentLine.slice(0,30) + '...';
                }
                contentLines.push(headerLine + ' ' + contentLine);
            }
        });
        return contentLines.join('<br/>');
    }

    giftOptionsTooltip.setTooltipContentLoaderFunction(getGiftOptionsTooltipContent);

    window.getGiftOptionsTooltipContent = getGiftOptionsTooltipContent;

//]]>

});
</script>
<?php endif; ?>
<?php endif; ?>
